#!/usr/bin/env python
#coding=utf-8
'''
Created on Jul 22, 2010

@author: lxd
'''
import wx
import yyWeb
from common.tools import Tools
from thread_yy import YYThread
import datetime

class PageIndex(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)  
        self.web = yyWeb.YYWeb(logfile='', interval_time=2)
        self.info = self.loadInfo()
        self.thread = None
        
        self.btnStart = wx.Button(self, -1, u'开始')  
        self.lstMessage = wx.ListBox(self, -1, size=(300, 300), style=wx.LB_SINGLE)   
        self.layout()
        
        self.Bind(wx.EVT_BUTTON, self.OnStartClick, self.btnStart)   
        
            
    def layout(self):
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.btnStart, 0, wx.ALL, 5)
        mainSizer.Add(self.lstMessage, 1, wx.ALL | wx.EXPAND, 5)
        self.SetSizer(mainSizer)
        mainSizer.Fit(self)
        mainSizer.SetSizeHints(self) 
        
    def loadInfo(self):
        #只取今天的
        info = [dict(name=u'', has_twitter=False, has_blog=False, has_daka=False, friends=[], message_num=0, renqi=0, over=False)]
        oldinfo = Tools.loadData(r'db/info')
        if oldinfo: #中断后自动记录当前用户名,发博与否，打卡与否，留言到哪个好友，第几次留言
            today = datetime.datetime.date(datetime.datetime.now())
            if oldinfo['time'] == today:
                info = oldinfo['info']
        return info
            
    def loadUser(self):
        user = Tools.loadData(r'db/user')
        if user:
            self.lstMessage.Append(u'要登录的用户：%s' % user)
        else:
            self.lstMessage.Append(u'请先进行用户管理')
        return user

    def loadRss(self):
        rss = Tools.loadData(r'db/rss')
        if rss:
            self.lstMessage.Append('%s' % rss)
        else:
            self.lstMessage.Append(u'请先进行博客管理')
        return rss
    
    def loadTMessage(self):
        message = Tools.loadData(r'db/tmessage')
        if message:
            self.lstMessage.Append('%s' % message)
        else:
            self.lstMessage.Append(u'请先进行迷你博客留言管理')
        return message
    
    def loadProxy(self):
        proxy = Tools.loadData(r'db/proxy')
        if proxy:
            self.lstMessage.Append(u'可以使用的代理：%s' % proxy)
        else:
            self.lstMessage.Append(u'请先设置代理')
        return proxy

    def OnStartClick(self, event):
        if self.btnStart.GetLabel() == u'开始':
            self.btnStart.SetLabel(u'停止')
      
            user = self.loadUser()                
            rss = self.loadRss()   
            tmessage = self.loadTMessage()   
            proxy = self.loadProxy()   
            info = self.info
            self.lstMessage.Append('%s' % info)
            
            if user and rss and info:        
                yyThread = YYThread(self, user, rss, tmessage, info, proxy)
                yyThread.setDaemon(True)  
                yyThread.start()                              
                self.thread = yyThread  
        else:
            if self.thread:
                self.thread.web.isSet = True
                self.thread.stop()
            self.btnStart.SetLabel(u'开始')

    def messageCall(self, msg):
        self.lstMessage.Append(msg) 
    
    def infoCall(self, info):
        self.info = info
        
    def btnStartCall(self, state):
        if state:
            self.btnStart.SetLabel(u'开始')
        else:
            self.btnStart.SetLabel(u'停止')
            
    def saveInfoData(self):
        today = datetime.datetime.date(datetime.datetime.now())
        Tools.saveData(r'db/info', dict(time=today, info=self.info))
                
    def OnClose(self, event):
        self.saveInfoData()

